package com.kchouse.myadapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.model.LatLng;
import com.kchouse.R;
import com.kchouse.StartActivity;
import com.kchouse.bean.BannerItem;
import com.kchouse.bean.MerchantItem;
import com.kchouse.bean.PrestoreItem;
import com.kchouse.common.MyUtils;
import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;

import java.io.File;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class PrestoreAdapter extends RecyclerView.Adapter<PrestoreAdapter.ViewHolder> {

    private Context context;
    private ArrayList<PrestoreItem> datas = null;
    private Intent intent = new Intent();


    public PrestoreAdapter(Context context, ArrayList<PrestoreItem> datas) {
        this.datas = datas;
        this.context = context;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

        View view = null;
        ViewHolder vh = null;
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lv_home_item, viewGroup, false);
        vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {


         PrestoreItem item = datas.get(position);
        if (TextUtils.isEmpty(item.getUrl())) {
            Picasso.with(context).load("null").error(R.mipmap.ic_loadingerror).into(viewHolder.iv_item);
        } else {
            Picasso.with(context).load(item.getUrl()).error(R.mipmap.ic_loadingerror).into(viewHolder.iv_item);
        }

        viewHolder.tv_title.setText(item.getTitle());
        viewHolder.tv_merchantName.setText(item.getMerchantShortName());
//        viewHolder.tv_price.setText(MyUtils.getdouble(item.getPrice()));
        viewHolder.tv_price.setText(item.getPrice()+"");
        viewHolder.tv_sales.setText("销量：" + (item.getSales() + item.getRealSales()));

        double distance = item.getDistance() / 1000;
        DecimalFormat df = new DecimalFormat("0.##");
        String st = df.format(distance);
        viewHolder.tv_distance.setText(st + "km");

//        viewHolder.tv_original.setText(MyUtils.getdouble(item.getOriginalPrice()));
        viewHolder.tv_original.setText(item.getOriginalPrice()+"");
        viewHolder.tv_original.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//添加下划线

        if (item.getCashNum() == 0) {
            viewHolder.tv_giving.setText("");
        } else {
            viewHolder.tv_giving.setText("最高抵扣" + (item.getCashNum() * 10) + "元");
        }

        if (item.isFollowStatus()) {
            viewHolder.tv_merchantName.setTextColor(context.getResources().getColor(R.color.text_main));
            viewHolder.tv_merchantName.setTypeface(Typeface.DEFAULT_BOLD);

        } else {
            viewHolder.tv_merchantName.setTypeface(Typeface.DEFAULT);
            viewHolder.tv_merchantName.setTextColor(context.getResources().getColor(R.color.colorblack));
        }
    }



    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
//        return 4;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_item;
        public TextView tv_title;
        public TextView tv_price;
        public TextView tv_sales;
        public TextView tv_distance;
        public TextView tv_merchantName;
        public TextView tv_original;
        public TextView tv_giving;

        public ViewHolder(View view) {
            super(view);
            iv_item = (ImageView) view.findViewById(R.id.iv_item);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_price = (TextView) view.findViewById(R.id.tv_price);
            tv_sales = (TextView) view.findViewById(R.id.tv_sales);
            tv_merchantName = (TextView) view.findViewById(R.id.tv_merchantName);
            tv_distance = (TextView) view.findViewById(R.id.tv_distance);
            tv_original = (TextView) view.findViewById(R.id.tv_original);
            tv_giving = (TextView) view.findViewById(R.id.tv_giving);
        }
    }


}
